
#include "entrys.h" 
#include "string.h" 
#include "storage.h" 
#include "outf.h" 
#include "debug.h" 
#include "memmap.h" 
#include "sscanf.h" 

/*  */
void entrys_init ( void )
{
	unsigned char b, buf[8];
	storage.Read(ADDR_PASSW_1, &b, 1 );
	if ( b >= STORAGE_FREE_ENTRY_TOKEN )
	{
		storage.Write(ADDR_PASSW_1, PASSW_1, StrLen(PASSW_1)+1);
		storage.Read(ADDR_PASSW_1, buf, StrLen(PASSW_1)+1);
		
		DBG("\n%s; PASSW_1: %s\n",__FUNCTION__,buf); 
	}
}

/* ======== DIRECTORY OF USER LIST 1 ======= */

#define   CONV_FOR_SAVE		0
#define   CONV_FOR_READ		1

static void phone_data_conversion ( storage_entry_phone_t *entry, char *data, int conversion_direction )
{		
	if ( conversion_direction == CONV_FOR_SAVE )
	{
		StrToBCD((unsigned char*)entry->phone, data, BCD_PHONE_SIZE);
		FlipFlopN((unsigned char*)entry->phone, BCD_PHONE_SIZE);
	}
	else
	{
		FlipFlopN((unsigned char*)entry->phone, BCD_PHONE_SIZE);
		BCDToStr((unsigned char*)data, (unsigned char*)entry->phone, BCD_PHONE_SIZE);
	}
}

 /* must be first function of the struct see storage_directory_properties_t in storage.h */
#define   MY_DIR_INDEX 	storage_index
#define   MY_DIR_PROP	directories[MY_DIR_INDEX]	

/* return -1 if fail */
int phone_add( void *s_phone)
{
	int free_entry_addr = storage_find_free(MY_DIR_INDEX);

	if ( free_entry_addr == STORAGE_ERR__NO_FRE_MEM )
		return STORAGE_ERR__NO_FRE_MEM; 

	storage_entry_phone_t entry;
	phone_data_conversion( &entry, (char*)s_phone, CONV_FOR_SAVE);

	DBG("\n%s; entry[0]: 0x%X\n",__FUNCTION__,entry.phone[0]); 


	/* don't add entry if the entry already exist */
	if ( phone_find(s_phone) != STORAGE_ERR__ENTRY_NOT_FOUND )
		return STORAGE_ERR__ENTRY_ALREADY_EXIST;

	return storage.Write(free_entry_addr, (unsigned char*)&entry, MY_DIR_PROP.entry_size);
}  

/* return addr of entry or STORAGE_ERR__ENTRY_NOT_FOUND */
int phone_find ( void *s_phone ) /* return addr of entry */ 
{
	unsigned int addr = MY_DIR_PROP.directory_addr;
	unsigned int end_addr = addr + (MY_DIR_PROP.entry_size * MY_DIR_PROP.entrys_count); 

	storage_entry_phone_t entry;

	phone_data_conversion( &entry, (char*)s_phone, CONV_FOR_SAVE);

	for ( ; addr < end_addr; addr += MY_DIR_PROP.entry_size )
	{
		if ( storage.comp(addr, &entry, MY_DIR_PROP.entry_size) == MY_DIR_PROP.entry_size )
		{
			return addr;
		}
	}
	
	return STORAGE_ERR__ENTRY_NOT_FOUND;
}

int phone_items_counter ( unsigned int list_name )
{
	return storage.entrys_counter(list_name);
}

void phone_print (unsigned int out_dev) 
{
	unsigned int addr = MY_DIR_PROP.directory_addr;
	unsigned int end_addr = addr + (MY_DIR_PROP.entry_size * MY_DIR_PROP.entrys_count); 

	char s_phone[24];
	storage_entry_phone_t entry;
	
	outf(out_dev, "\n ==== PRINT LIST #%d ==== List size max: %d\n\n", MY_DIR_INDEX, MY_DIR_PROP.entrys_count);
	
	int i = 0;//,j = 0;

	for ( ; addr < end_addr; addr += MY_DIR_PROP.entry_size )
	{
		if ( storage.Read(addr, &entry, MY_DIR_PROP.entry_size) == MY_DIR_PROP.entry_size )
		{
			//DBG("\n%s; %d] entry[0]: 0x%X\n",__FUNCTION__,++j,entry.phone[0]); 

			if ( entry.phone[0] < STORAGE_FREE_ENTRY_TOKEN )
			{
				phone_data_conversion( &entry, s_phone, CONV_FOR_READ ); 
				
				outf(out_dev, " %d) %s\n", ++i, s_phone);
			}
		}
	}
	
	outf(out_dev, "\n == END OF PRINT LIST #%d ==\n\n", MY_DIR_INDEX);
}

















/* /\* return 1 if OK or STORAGE_ERR__ENTRY_NOT_FOUND in other case *\/ */
/* int phone_delete (void *ref ) /\* return addr of entry *\/  */
/* { */
/* 	int del_addr = phone_find(ref); */
	
/* 	if ( del_addr == STORAGE_ERR__ENTRY_NOT_FOUND )  */
/* 		return STORAGE_ERR__ENTRY_NOT_FOUND; */
	
/* 	unsigned char wb = STORAGE_FREE_ENTRY_TOKEN; */
	
/* 	return storage.Write(del_addr, &wb, 1); */
/* } */
